###
#
# Data Localization Project
# 
# Data Analysis
#
# Authors: Tyler Girard and Alex Wilhelm
#
#
#
###

#------------------------------------------------------------------------------------------------------------------------------------------------------------#
#------------------------------------------------------------------------------------------------------------------------------------------------------------#
#### Load Packages ####
#------------------------------------------------------------------------------------------------------------------------------------------------------------#
#------------------------------------------------------------------------------------------------------------------------------------------------------------#

library(here)
library(tidyverse)
library(rio)
library(marginaleffects)
library(modelsummary)
library(gt)
library(ggpubr)

library(table1)
library(flextable)



#------------------------------------------------------------------------------------------------------------------------------------------------------------#
#------------------------------------------------------------------------------------------------------------------------------------------------------------#
#### Set Seed ####
#------------------------------------------------------------------------------------------------------------------------------------------------------------#
#------------------------------------------------------------------------------------------------------------------------------------------------------------#

set.seed(515)

#------------------------------------------------------------------------------------------------------------------------------------------------------------#
#------------------------------------------------------------------------------------------------------------------------------------------------------------#
#### Load Data ####
#------------------------------------------------------------------------------------------------------------------------------------------------------------#
#------------------------------------------------------------------------------------------------------------------------------------------------------------#

df <- import(here("1 Data", "Data for Analysis.RData"))

names(df)

table(df$Status)


#------------------------------------------------------------------------------------------------------------------------------------------------------------#
#------------------------------------------------------------------------------------------------------------------------------------------------------------#
#### Prep Data ####
#------------------------------------------------------------------------------------------------------------------------------------------------------------#
#------------------------------------------------------------------------------------------------------------------------------------------------------------#


dfexp <- df %>%
  mutate(across(where(is.character), ~na_if(., ""))) %>%
  mutate(across(where(is.character), ~na_if(., "-99"))) %>%
  mutate(ac1 = case_when(ac1 == "Yellow" ~ 1,
                         TRUE ~ 0),
         expmc = case_when(exp2 == "canada" & mc == "Canada" ~ 1,
                           exp2 == "china" & mc == "China" ~ 1,
                           exp2 == "south korea" & mc == "South Korea" ~ 1,
                           TRUE ~ 0)) %>%
  mutate(pid = case_when(party_id == "Democrat" ~ "Dem",
                         party_id == "Republican" ~ "Rep",
                         party_id == "Independent" ~ "Ind")) %>%
  mutate(ideo = extract_numeric(ideological_self_1)) %>%
  mutate(votechoice = case_when(pres_vote_2020 == "Donald Trump" ~ "Trump",
                                pres_vote_2020 == "Joe Biden" ~ "Biden",
                                pres_vote_2020 == "Did not vote" ~ "Other",
                                pres_vote_2020 == "Other (please specify)" ~ "Other")) %>%
  mutate(sex_binary = case_when(sex == "A man" ~ "Male",
                                TRUE ~ "Female/Other")) %>%
  mutate(psdegree = case_when(education == "Graduate degree" ~ "Yes",
                              education == "4-year college degree" ~ "Yes",
                              education == "2-year college degree" ~ "Yes",
                              education == "Some college (no degree)" ~ "No",
                              education == "High school diploma / GED" ~ "No",
                              education == "Less than a high school diploma" ~ "No"),
         age_cat = case_when(age >= 18 & age <=24 ~ "18-24",
                             age >= 25 & age <=34 ~ "25-34",
                             age >= 35 & age <=44 ~ "35-44",
                             age >= 45 & age <=54 ~ "45-54",
                             age >= 55 & age <=64 ~ "55-64",
                             age >= 65 ~ "65+"),
         region = case_when(state_of_residence == "Indiana" ~ "Midwest",
                            state_of_residence == "Illinois" ~ "Midwest",
                            state_of_residence == "Michigan" ~ "Midwest",
                            state_of_residence == "Ohio" ~ "Midwest",
                            state_of_residence == "Wisconsin" ~ "Midwest",
                            state_of_residence == "Iowa" ~ "Midwest",
                            state_of_residence == "Nebraska" ~ "Midwest",
                            state_of_residence == "Kansas" ~ "Midwest",
                            state_of_residence == "North Dakota" ~ "Midwest",
                            state_of_residence == "Minnesota" ~ "Midwest",
                            state_of_residence == "South Dakota" ~ "Midwest",
                            state_of_residence == "Missouri" ~ "Midwest",
                            state_of_residence == "Connecticut" ~ "Northeast",
                            state_of_residence == "Maine" ~ "Northeast",
                            state_of_residence == "Massachusetts" ~ "Northeast",
                            state_of_residence == "New Hampshire" ~ "Northeast",
                            state_of_residence == "Rhode Island" ~ "Northeast",
                            state_of_residence == "Vermont" ~ "Northeast",
                            state_of_residence == "New Jersey" ~ "Northeast",
                            state_of_residence == "New York" ~ "Northeast",
                            state_of_residence == "Pennsylvania" ~ "Northeast",
                            state_of_residence == "Delaware" ~ "South",
                            state_of_residence == "District of Columbia" ~ "South",
                            state_of_residence == "Florida" ~ "South",
                            state_of_residence == "Georgia" ~ "South",
                            state_of_residence == "Maryland" ~ "South",
                            state_of_residence == "North Carolina" ~ "South",
                            state_of_residence == "South Carolina" ~ "South",
                            state_of_residence == "Virginia" ~ "South",
                            state_of_residence == "West Virginia" ~ "South",
                            state_of_residence == "Alabama" ~ "South",
                            state_of_residence == "Kentucky" ~ "South",
                            state_of_residence == "Mississippi" ~ "South",
                            state_of_residence == "Tennessee" ~ "South",
                            state_of_residence == "Arkansas" ~ "South",
                            state_of_residence == "Louisiana" ~ "South",
                            state_of_residence == "Oklahoma" ~ "South",
                            state_of_residence == "Texas" ~ "South",
                            state_of_residence == "Arizona" ~ "West",
                            state_of_residence == "Colorado" ~ "West",
                            state_of_residence == "Idaho" ~ "West",
                            state_of_residence == "New Mexico" ~ "West",
                            state_of_residence == "Montana" ~ "West",
                            state_of_residence == "Utah" ~ "West",
                            state_of_residence == "Nevada" ~ "West",
                            state_of_residence == "Wyoming" ~ "West",
                            state_of_residence == "Alaska" ~ "West",
                            state_of_residence == "California" ~ "West",
                            state_of_residence == "Hawaii" ~ "West",
                            state_of_residence == "Oregon" ~ "West",
                            state_of_residence == "Washington" ~ "West"))

table(dfexp$expmc)
summary(dfexp$expmc)


dfsdo <- dfexp %>%
  select(rid, contains("social")) %>%
  select(-c("social_dominance_DO")) %>%
  mutate(social_dominance_1 = as.numeric(social_dominance_1),
         social_dominance_2 = as.numeric(social_dominance_2),
         social_dominance_3 = as.numeric(social_dominance_3),
         social_dominance_4 = as.numeric(social_dominance_4)) %>%
  mutate(social_dominance_1 = social_dominance_1*-1 + 10,
         social_dominance_3 = social_dominance_3*-1 + 10)

head(dfsdo)

summary(dfsdo)

psych::alpha(scale(dfsdo[,-1]))
# cronbach's alpha = 0.64


dfsdo$sdo_scale <- apply(dfsdo[,-1], 1, function(x) mean(x, na.rm = TRUE))
summary(dfsdo$sdo_scale)

plot(hist(dfsdo$sdo_scale))

dfsdo$sdo_scale_bin <- ifelse(dfsdo$sdo_scale >= 3.25, "HIGH",
                              ifelse(dfsdo$sdo_scale < 3.25, "LOW", NA))

dfsdo <- dfsdo %>%
  select(rid, sdo_scale, sdo_scale_bin)

#------------------------------------------------------------------------------------------------------------------------------------------------------------#
#------------------------------------------------------------------------------------------------------------------------------------------------------------#
#### Descriptive Stats ####
#------------------------------------------------------------------------------------------------------------------------------------------------------------#
#------------------------------------------------------------------------------------------------------------------------------------------------------------#


ds <- dfexp %>%
  left_join(dfsdo) %>%
  select(sex_binary, age_cat, psdegree, income, region,
         sdo_scale_bin, pid, votechoice, ideo,
         ac1, expmc) %>%
  mutate(ac1 = case_when(ac1 == 1 ~ "Pass",
                             ac1 == 0 ~ "Fail"),
         expmc = case_when(expmc == 1 ~ "Pass",
                           expmc == 0 ~ "Fail"))


head(ds)

label(ds$sex_binary) <- "Sex"
label(ds$age_cat) <- "Age"
label(ds$psdegree) <- "Post-Secondary Degree"
label(ds$income) <- "Income"
label(ds$region) <- "Region"
label(ds$sdo_scale_bin) <- "Social Dominance Orientation"
label(ds$pid) <- "Party ID"
label(ds$votechoice) <- "Presidential Vote Choice (2020)"
label(ds$ideo) <- "Ideological Self-Placement"
label(ds$ac1) <- "Pre-Treatment Attention Check"
label(ds$expmc) <- "Post-Treatment Manipulation Check"

dstable <- table1(~ sex_binary + age_cat + psdegree + income + region + 
         sdo_scale_bin + pid + votechoice + ideo +
         ac1 + expmc, 
       data = ds,
       topclass = "Rtable1-zebra")


t1flex(dstable) %>%
  save_as_docx(path = here("3 Tables", "Descriptive Statistics Table.docx"))

#------------------------------------------------------------------------------------------------------------------------------------------------------------#
#------------------------------------------------------------------------------------------------------------------------------------------------------------#
#### Prep Exp 1 ####
#------------------------------------------------------------------------------------------------------------------------------------------------------------#
#------------------------------------------------------------------------------------------------------------------------------------------------------------#


### control 

dfexp1_control <- dfexp %>%
  select(rid, exp1, contains("exp1_control")) %>%
  select(-c(contains("DO")))

head(dfexp1_control)

dfexp1_control <- dfexp1_control %>%
  mutate(out_exp1_q1 = case_when(exp1_control_q1 == "Strongly support" ~ 2,
                                 exp1_control_q1 == "Somewhat support" ~ 1,
                                 exp1_control_q1 == "Neither support nor oppose" ~ 0,
                                 exp1_control_q1 == "Somewhat oppose" ~ -1,
                                 exp1_control_q1 == "Strongly oppose" ~ -2),
         
         out_exp1_q2 = case_when(exp1_control_q2 == "Strongly support" ~ 2,
                                 exp1_control_q2 == "Somewhat support" ~ 1,
                                 exp1_control_q2 == "Neither support nor oppose" ~ 0,
                                 exp1_control_q2 == "Somewhat oppose" ~ -1,
                                 exp1_control_q2 == "Strongly oppose" ~ -2),
         
         out_exp1_q3 = case_when(exp1_control_q3 == "Very fair" ~ 2,
                                 exp1_control_q3 == "Somewhat fair" ~ 1,
                                 exp1_control_q3 == "Neither fair nor unfair" ~ 0,
                                 exp1_control_q3 == "Somewhat unfair" ~ -1,
                                 exp1_control_q3 == "Very unfair" ~ -2))


table(dfexp1_control$exp1_control_q1, dfexp1_control$out_exp1_q1)
table(dfexp1_control$exp1_control_q2, dfexp1_control$out_exp1_q2)
table(dfexp1_control$exp1_control_q3, dfexp1_control$out_exp1_q3)

dfexp1_control <- dfexp1_control %>%
  select(rid, exp1, contains("out_")) %>%
  filter(exp1 == "control")

head(dfexp1_control)



### protectionism 

dfexp1_protect <- dfexp %>%
  select(rid, exp1, contains("exp1_protect")) %>%
  select(-c(contains("DO")))

head(dfexp1_protect)

dfexp1_protect <- dfexp1_protect %>%
  mutate(out_exp1_q1 = case_when(exp1_protect_q1 == "Strongly support" ~ 2,
                                 exp1_protect_q1 == "Somewhat support" ~ 1,
                                 exp1_protect_q1 == "Neither support nor oppose" ~ 0,
                                 exp1_protect_q1 == "Somewhat oppose" ~ -1,
                                 exp1_protect_q1 == "Strongly oppose" ~ -2),
         
         out_exp1_q2 = case_when(exp1_protect_q2 == "Strongly support" ~ 2,
                                 exp1_protect_q2 == "Somewhat support" ~ 1,
                                 exp1_protect_q2 == "Neither support nor oppose" ~ 0,
                                 exp1_protect_q2 == "Somewhat oppose" ~ -1,
                                 exp1_protect_q2 == "Strongly oppose" ~ -2),
         
         out_exp1_q3 = case_when(exp1_protect_q3 == "Very fair" ~ 2,
                                 exp1_protect_q3 == "Somewhat fair" ~ 1,
                                 exp1_protect_q3 == "Neither fair nor unfair" ~ 0,
                                 exp1_protect_q3 == "Somewhat unfair" ~ -1,
                                 exp1_protect_q3 == "Very unfair" ~ -2))


table(dfexp1_protect$exp1_protect_q1, dfexp1_protect$out_exp1_q1)
table(dfexp1_protect$exp1_protect_q2, dfexp1_protect$out_exp1_q2)
table(dfexp1_protect$exp1_protect_q3, dfexp1_protect$out_exp1_q3)

dfexp1_protect <- dfexp1_protect %>%
  select(rid, exp1, contains("out_")) %>%
  filter(exp1 == "protectionism")

head(dfexp1_protect)


### sovereignty 

dfexp1_sov <- dfexp %>%
  select(rid, exp1, contains("exp1_sov")) %>%
  select(-c(contains("DO")))

head(dfexp1_sov)

dfexp1_sov <- dfexp1_sov %>%
  mutate(out_exp1_q1 = case_when(exp1_sov_q1 == "Strongly support" ~ 2,
                                 exp1_sov_q1 == "Somewhat support" ~ 1,
                                 exp1_sov_q1 == "Neither support nor oppose" ~ 0,
                                 exp1_sov_q1 == "Somewhat oppose" ~ -1,
                                 exp1_sov_q1 == "Strongly oppose" ~ -2),
         
         out_exp1_q2 = case_when(exp1_sov_q2 == "Strongly support" ~ 2,
                                 exp1_sov_q2 == "Somewhat support" ~ 1,
                                 exp1_sov_q2 == "Neither support nor oppose" ~ 0,
                                 exp1_sov_q2 == "Somewhat oppose" ~ -1,
                                 exp1_sov_q2 == "Strongly oppose" ~ -2),
         
         out_exp1_q3 = case_when(exp1_sov_q3 == "Very fair" ~ 2,
                                 exp1_sov_q3 == "Somewhat fair" ~ 1,
                                 exp1_sov_q3 == "Neither fair nor unfair" ~ 0,
                                 exp1_sov_q3 == "Somewhat unfair" ~ -1,
                                 exp1_sov_q3 == "Very unfair" ~ -2))


table(dfexp1_sov$exp1_sov_q1, dfexp1_sov$out_exp1_q1)
table(dfexp1_sov$exp1_sov_q2, dfexp1_sov$out_exp1_q2)
table(dfexp1_sov$exp1_sov_q3, dfexp1_sov$out_exp1_q3)

dfexp1_sov <- dfexp1_sov %>%
  select(rid, exp1, contains("out_")) %>%
  filter(exp1 == "sovereignty")

head(dfexp1_sov)


### make df

df_covars <- dfexp %>%
  select(rid, ac1, pid, ideo, votechoice, sex_binary, psdegree, age_cat, region)


dfexp1 <- rbind(dfexp1_control, dfexp1_protect, dfexp1_sov) %>%
  left_join(dfsdo) %>%
  left_join(df_covars)

dfexp1_passac <- dfexp1 %>%
  filter(ac1 == 1)


#------------------------------------------------------------------------------------------------------------------------------------------------------------#
#------------------------------------------------------------------------------------------------------------------------------------------------------------#
#### Analysis Exp 1 ####
#------------------------------------------------------------------------------------------------------------------------------------------------------------#
#------------------------------------------------------------------------------------------------------------------------------------------------------------#

m1 <- lm(out_exp1_q1 ~ exp1, data = dfexp1_passac)
m2 <- lm(out_exp1_q2 ~ exp1, data = dfexp1_passac)
m3 <- lm(out_exp1_q3 ~ exp1, data = dfexp1_passac)

# note: the effects in m1 and m3 are directly cited in the main text (beginning of Section 5: Results)
summary(m1)
summary(m2)
summary(m3)

m1_completedata <- lm(out_exp1_q1 ~ exp1, data = dfexp1)
m2_completedata <- lm(out_exp1_q2 ~ exp1, data = dfexp1)
m3_completedata <- lm(out_exp1_q3 ~ exp1, data = dfexp1)

summary(m1_completedata)
summary(m2_completedata)
summary(m3_completedata)




#------------------------------------------------------------------------------------------------------------------------------------------------------------#
#------------------------------------------------------------------------------------------------------------------------------------------------------------#
#### ~ Figures ~ ####
#------------------------------------------------------------------------------------------------------------------------------------------------------------#
#------------------------------------------------------------------------------------------------------------------------------------------------------------#


cplot1 <- broom::tidy(m1, conf.int = TRUE, conf.level = 0.95) %>%
  mutate(exp1 = case_when(term == "exp1protectionism" ~ "US Economic\nCosts",
                          term == "exp1sovereignty" ~ "Foreign\nSovereignty")) %>%
  mutate(model = "Do you support or oppose other countries adopting data localization laws?")

cplot1


cplot2 <- broom::tidy(m2, conf.int = TRUE, conf.level = 0.95)%>%
  mutate(exp1 = case_when(term == "exp1protectionism" ~ "US Economic\nCosts",
                          term == "exp1sovereignty" ~ "Foreign\nSovereignty")) %>%
  mutate(model = "Do you support or oppose the US government pressuring other countries
    to remove their data localization laws?")
cplot2

cplot3 <- broom::tidy(m3, conf.int = TRUE, conf.level = 0.95)%>%
  mutate(exp1 = case_when(term == "exp1protectionism" ~ "US Economic\nCosts",
                          term == "exp1sovereignty" ~ "Foreign\nSovereignty")) %>%
  mutate(model = "Are data localization laws fair or unfair to American technology companies?")
cplot3

cplot <- rbind(cplot1, cplot2, cplot3) %>%
  drop_na(exp1) %>%
  mutate(model = fct_relevel(model,
                             "Do you support or oppose other countries adopting data localization laws?",
                             "Do you support or oppose the US government pressuring other countries
    to remove their data localization laws?",
                             "Are data localization laws fair or unfair to American technology companies?"))

plot_cplot <- ggplot(cplot, aes(x = estimate, xmin = conf.low, xmax = conf.high, y = exp1)) +
  geom_vline(xintercept = 0, colour = "indianred", lty = "dashed") +
  geom_point(size = 3) +
  geom_linerange(size = 1) +
  scale_x_continuous(limits = c(-0.3, 0.3)) +
  facet_wrap(~model, ncol = 1) +
  theme_bw() +
  theme(
    strip.background = element_rect(fill = "#F3F4F9"),
    strip.text = element_text(face = "bold")
  ) +
  labs(
    x = "Estimate",
    y = ""
  )

plot_cplot

ggsave(filename = here("2 Figures", "Experiment 1 Coefficient Plot (Presentation).png"), plot = plot_cplot, scale = 1.1)

plot_cplot_paper <- ggplot(cplot, aes(x = estimate, xmin = conf.low, xmax = conf.high, y = exp1)) +
  geom_vline(xintercept = 0, colour = "indianred", lty = "dashed") +
  geom_point(size = 3) +
  geom_linerange(size = 1) +
  scale_x_continuous(limits = c(-0.3, 0.3)) +
  scale_y_discrete(labels = c("US Economic\nCosts" = "Digital Protectionism", "Foreign\nSovereignty"="Digital Sovereignty")) +
  facet_wrap(~model, ncol = 1) +
  theme_bw() +
  theme(
    strip.background = element_rect(fill = "#F3F4F9"),
    strip.text = element_text(face = "bold")
  ) +
  labs(
    x = "Estimate",
    y = ""
  )

plot_cplot_paper

ggsave(filename = here("2 Figures", "Experiment 1 Coefficient Plot (Paper).png"), plot = plot_cplot_paper, scale = 1.1)


#------------------------------------------------------------------------------------------------------------------------------------------------------------#
#------------------------------------------------------------------------------------------------------------------------------------------------------------#
#### ~ Tables ~ ####
#------------------------------------------------------------------------------------------------------------------------------------------------------------#
#------------------------------------------------------------------------------------------------------------------------------------------------------------#


modsac <- list(
  "Support Use of Data Localization Policies" = m1, 
  "Support Use of Data Localization Policies" = m1_completedata, 
  "Support US Govt Pressuring for Removal" = m2,
  "Support US Govt Pressuring for Removal" = m2_completedata,
  "Fair to American Tech Firms" = m3,
  "Fair to American Tech Firms" = m3_completedata
)


modsac <- list(
  m1, 
  m1_completedata, 
  m2,
  m2_completedata,
  m3,
  m3_completedata
)

cm <- c(
  '(Intercept)'='Intercept',
  'exp1protectionism'='Digital Protectionism',
  'exp1sovereignty'='Digital Sovereignty'
)

rows <- tribble(~term,          ~"(1)",  ~"(2)", ~"(3)",  ~"(4)", ~"(5)",  ~"(6)", 
                'Attention Check',         'Pass', 'All','Pass', 'All','Pass', 'All')
attr(rows, 'position') <- c(5)

tab <- modelsummary(modsac,
             output = "gt",
             stars = TRUE,
             coef_map = cm,
             estimate = "{estimate} ({std.error}){stars}",
             statistic = NULL,
             add_rows = rows,
             gof_map = c("nobs", "r.squared"))

tab <- tab %>%
  # column labels
  tab_spanner(label = 'Support Use of Data Localization Policies', columns = 2:3) %>%
  tab_spanner(label = 'Support US Govt Pressuring for Removal', columns = 4:5) %>%
  tab_spanner(label = 'Fair to American Tech Firms', columns = 6:7)

tab
  

gt::gtsave(tab, filename = here("3 Tables", "Experiment 1 Table.docx"))


#------------------------------------------------------------------------------------------------------------------------------------------------------------#
#------------------------------------------------------------------------------------------------------------------------------------------------------------#
#### ~~~ Subgroups ~~~ ####
#------------------------------------------------------------------------------------------------------------------------------------------------------------#
#------------------------------------------------------------------------------------------------------------------------------------------------------------#

names(dfexp1)

# use dataframe with respondents who passed AC

het1_vote <- lm(out_exp1_q1 ~ exp1*votechoice, data = dfexp1_passac)
het1_pid <- lm(out_exp1_q1 ~ exp1*pid, data = dfexp1_passac)
het1_ideo <- lm(out_exp1_q1 ~ exp1*ideo, data = dfexp1_passac)
het1_sdo <- lm(out_exp1_q1 ~ exp1*sdo_scale_bin, data = dfexp1_passac)
het1_sex <- lm(out_exp1_q1 ~ exp1*sex_binary, data = dfexp1_passac)

summary(het1_vote)
summary(het1_pid)
summary(het1_ideo)
summary(het1_sdo)
summary(het1_sex)

het2_vote <- lm(out_exp1_q2 ~ exp1*votechoice, data = dfexp1_passac)
het2_pid <- lm(out_exp1_q2 ~ exp1*pid, data = dfexp1_passac)
het2_ideo <- lm(out_exp1_q2 ~ exp1*ideo, data = dfexp1_passac)
het2_sdo <- lm(out_exp1_q2 ~ exp1*sdo_scale_bin, data = dfexp1_passac)
het2_sex <- lm(out_exp1_q2 ~ exp1*sex_binary, data = dfexp1_passac)

summary(het2_vote)
summary(het2_pid)
summary(het2_ideo)
summary(het2_sdo)
summary(het2_sex)

het3_vote <- lm(out_exp1_q3 ~ exp1*votechoice, data = dfexp1_passac)
het3_pid <- lm(out_exp1_q3 ~ exp1*pid, data = dfexp1_passac)
het3_ideo <- lm(out_exp1_q3 ~ exp1*ideo, data = dfexp1_passac)
het3_sdo <- lm(out_exp1_q3 ~ exp1*sdo_scale_bin, data = dfexp1_passac)
het3_sex <- lm(out_exp1_q3 ~ exp1*sex_binary, data = dfexp1_passac)

summary(het3_vote)
summary(het3_pid)
summary(het3_ideo)
summary(het3_sdo)
summary(het3_sex)


# create plot for SI File

hetplot1 <- plot_predictions(het1_ideo, by = c("ideo", "exp1"), draw = TRUE) +
  theme_bw() +
  scale_y_continuous(limits = c(-0.5,1))+
  labs(
    x = "Ideology (0 - 10)",
    y = "Predicted Value (-2 - 2)",
    title = "Support Use of Data Localization \nPolicies",
    fill = "Treatment Group",
    color = "Treatment Group"
  )

hetplot2 <- plot_predictions(het2_ideo, by = c("ideo", "exp1"), draw = TRUE) +
  theme_bw() +
  scale_y_continuous(limits = c(-0.5,1))+
  theme(legend.title = element_blank())+
  labs(
    x = "Ideology (0 - 10)",
    y = "Predicted Value (-2 - 2)",
    title = "Support US Govt Pressuring for \nRemoval",
    fill = "Treatment Group",
    color = "Treatment Group"
  )

hetplot3 <- plot_predictions(het3_ideo, by = c("ideo", "exp1"), draw = TRUE) +
  theme_bw() +
  scale_y_continuous(limits = c(-0.5,1))+
  theme(legend.title = element_blank())+
  labs(
    x = "Ideology (0 - 10)",
    y = "Predicted Value (-2 - 2)",
    title = "Fair to American Tech Firms",
    fill = "Treatment Group",
    color = "Treatment Group"
  )

ggarrange(hetplot1, hetplot2, hetplot3,
          common.legend = TRUE,
          legend = "bottom")

hetplot_combo <- ggarrange(hetplot1, hetplot2, hetplot3,
                           #ncol = 2,
                           common.legend = TRUE,
                           legend = "bottom")

ggsave(filename = here("2 Figures", "Experiment 1 Effects Plot with Ideology (SI File).png"), plot = hetplot_combo, scale = 1.3)

# tables for SI File


### VOTE CHOICE

modshet_vote <- list(
  "Support Use of Data Localization Policies" = het1_vote, 
  "Support US Govt Pressuring for Removal" = het2_vote, 
  "Fair to American Tech Firms" = het3_vote
)

modelsummary(modshet_vote,
             #output = "gt",
             stars = TRUE)

cm <- c(
  '(Intercept)'='Intercept',
  'exp1protectionism'='Digital Protectionism',
  'exp1sovereignty'='Digital Sovereignty',
  'votechoiceOther'='Vote Choice (Other)',
  'votechoiceTrump'='Vote Choice (Trump)',
  'exp1protectionism:votechoiceOther'='Digital Protectionism x Vote Choice (Other)',
  'exp1sovereignty:votechoiceOther'='Digital Sovereignty x Vote Choice (Other)',
  'exp1protectionism:votechoiceTrump'='Digital Protectionism x Vote Choice (Trump)',
  'exp1sovereignty:votechoiceTrump'='Digital Sovereignty x Vote Choice (Trump)'
  
)

tab_hetvote <- modelsummary(modshet_vote,
                    output = "gt",
                    stars = TRUE,
                    coef_map = cm,
                    estimate = "{estimate} ({std.error}){stars}",
                    statistic = NULL,
                    #add_rows = rows,
                    gof_map = c("nobs", "r.squared"))

gt::gtsave(tab_hetvote, filename = here("3 Tables", "Experiment 1 Table (Subgroup - Vote Choice).docx"))


### PID

modshet_pid <- list(
  "Support Use of Data Localization Policies" = het1_pid, 
  "Support US Govt Pressuring for Removal" = het2_pid, 
  "Fair to American Tech Firms" = het3_pid
)

modelsummary(modshet_pid,
             #output = "gt",
             stars = TRUE)

cm <- c(
  '(Intercept)'='Intercept',
  'exp1protectionism'='Digital Protectionism',
  'exp1sovereignty'='Digital Sovereignty',
  'pidInd'='Party ID (Ind.)',
  'pidRep'='Party ID (Rep.)',
  'exp1protectionism:pidInd'='Digital Protectionism x Party ID (Ind.)',
  'exp1sovereignty:pidInd'='Digital Sovereignty x Party ID (Ind.)',
  'exp1protectionism:pidRep'='Digital Protectionism x Party ID (Rep.)',
  'exp1sovereignty:pidRep'='Digital Sovereignty x Party ID (Rep.)'
  
)

tab_hetpid <- modelsummary(modshet_pid,
                            output = "gt",
                            stars = TRUE,
                            coef_map = cm,
                            estimate = "{estimate} ({std.error}){stars}",
                            statistic = NULL,
                            #add_rows = rows,
                            gof_map = c("nobs", "r.squared"))

gt::gtsave(tab_hetpid, filename = here("3 Tables", "Experiment 1 Table (Subgroup - PID).docx"))


### IDEOLOGY

modshet_ideo <- list(
  "Support Use of Data Localization Policies" = het1_ideo, 
  "Support US Govt Pressuring for Removal" = het2_ideo, 
  "Fair to American Tech Firms" = het3_ideo
)

modelsummary(modshet_ideo,
             #output = "gt",
             stars = TRUE)

cm <- c(
  '(Intercept)'='Intercept',
  'exp1protectionism'='Digital Protectionism',
  'exp1sovereignty'='Digital Sovereignty',
  'ideo'='Left-Right Ideology (0-10)',
  'exp1protectionism:ideo'='Digital Protectionism x Left-Right Ideology (0-10)',
  'exp1sovereignty:ideo'='Digital Sovereignty x Left-Right Ideology (0-10)'
  
)

tab_hetideo <- modelsummary(modshet_ideo,
                            output = "gt",
                            stars = TRUE,
                            coef_map = cm,
                            estimate = "{estimate} ({std.error}){stars}",
                            statistic = NULL,
                            #add_rows = rows,
                            gof_map = c("nobs", "r.squared"))

gt::gtsave(tab_hetideo, filename = here("3 Tables", "Experiment 1 Table (Subgroup - Ideology).docx"))



### SDO

modshet_sdo <- list(
  "Support Use of Data Localization Policies" = het1_sdo, 
  "Support US Govt Pressuring for Removal" = het2_sdo, 
  "Fair to American Tech Firms" = het3_sdo
)

modelsummary(modshet_sdo,
             #output = "gt",
             stars = TRUE)

cm <- c(
  '(Intercept)'='Intercept',
  'exp1protectionism'='Digital Protectionism',
  'exp1sovereignty'='Digital Sovereignty',
  'sdo_scale_binLOW'='Social Dominance Orientation (LOW)',
  'exp1protectionism:sdo_scale_binLOW'='Digital Protectionism x Social Dominance Orientation (LOW))',
  'exp1sovereignty:sdo_scale_binLOW'='Digital Sovereignty x Social Dominance Orientation (LOW)'
  
)

tab_hetsdo <- modelsummary(modshet_sdo,
                            output = "gt",
                            stars = TRUE,
                            coef_map = cm,
                            estimate = "{estimate} ({std.error}){stars}",
                            statistic = NULL,
                            #add_rows = rows,
                            gof_map = c("nobs", "r.squared"))

gt::gtsave(tab_hetsdo, filename = here("3 Tables", "Experiment 1 Table (Subgroup - SDO).docx"))




### Sex

modshet_sex <- list(
  "Support Use of Data Localization Policies" = het1_sex, 
  "Support US Govt Pressuring for Removal" = het2_sex, 
  "Fair to American Tech Firms" = het3_sex
)

modelsummary(modshet_sex,
             #output = "gt",
             stars = TRUE)

cm <- c(
  '(Intercept)'='Intercept',
  'exp1protectionism'='Digital Protectionism',
  'exp1sovereignty'='Digital Sovereignty',
  'sex_binaryMale'='Gender (Man)',
  'exp1protectionism:sex_binaryMale'='Digital Protectionism x Gender (Man)',
  'exp1sovereignty:sex_binaryMale'='Digital Sovereignty x Gender (Man)'
  
)

tab_hetsex <- modelsummary(modshet_sex,
                           output = "gt",
                           stars = TRUE,
                           coef_map = cm,
                           estimate = "{estimate} ({std.error}){stars}",
                           statistic = NULL,
                           #add_rows = rows,
                           gof_map = c("nobs", "r.squared"))

gt::gtsave(tab_hetsex, filename = here("3 Tables", "Experiment 1 Table (Subgroup - Gender).docx"))










#------------------------------------------------------------------------------------------------------------------------------------------------------------#
#------------------------------------------------------------------------------------------------------------------------------------------------------------#
#### Prep Exp 2 ####
#------------------------------------------------------------------------------------------------------------------------------------------------------------#
#------------------------------------------------------------------------------------------------------------------------------------------------------------#


dfexp2all <- dfexp %>%
  select(rid, contains("exp2")) %>%
  select(-c(contains("_DO")))

names(dfexp2all)

dfexp2q1 <- dfexp %>%
  select(rid, contains("exp2")) %>%
  select(rid, contains("q1")) %>%
  pivot_longer(cols = -c(rid)) %>%
  mutate(out_exp2_q1 = case_when(value == "Strongly support" ~ 2,
                                 value == "Somewhat support" ~ 1,
                                 value == "Neither support nor oppose" ~ 0,
                                 value == "Somewhat oppose" ~ -1,
                                 value == "Strongly oppose" ~ -2)) %>%
  select(-c(name, value)) %>%
  drop_na(out_exp2_q1)

head(dfexp2q1)


names(dfexp2all)

dfexp2q2 <- dfexp %>%
  select(rid, contains("exp2")) %>%
  select(rid, contains("q2")) %>%
  pivot_longer(cols = -c(rid)) %>%
  mutate(out_exp2_q2 = case_when(value == "Very fair" ~ 2,
                                 value == "Somewhat fair" ~ 1,
                                 value == "Neither fair nor unfair" ~ 0,
                                 value == "Somewhat unfair" ~ -1,
                                 value == "Very unfair" ~ -2)) %>%
  select(-c(name, value)) %>%
  drop_na(out_exp2_q2)

head(dfexp2q2)


dfexp2q3 <- dfexp %>%
  select(rid, contains("exp2")) %>%
  select(rid, contains("q3")) %>%
  pivot_longer(cols = -c(rid)) %>%
  mutate(out_exp2_q3 = case_when(value == "Very fair" ~ 2,
                                 value == "Somewhat fair" ~ 1,
                                 value == "Neither fair nor unfair" ~ 0,
                                 value == "Somewhat unfair" ~ -1,
                                 value == "Very unfair" ~ -2)) %>%
  select(-c(name, value)) %>%
  drop_na(out_exp2_q3)

head(dfexp2q3)


dfexp2 <- dfexp %>%
  select(rid, exp1, exp2) %>%
  left_join(dfexp2q1) %>%
  left_join(dfexp2q2) %>%
  left_join(dfexp2q3)%>%
  left_join(dfsdo) %>%
  left_join(df_covars)


head(dfexp2)

dfexp2_passac <- dfexp2 %>%
  filter(ac1 == 1)


#------------------------------------------------------------------------------------------------------------------------------------------------------------#
#------------------------------------------------------------------------------------------------------------------------------------------------------------#
#### Analysis Exp 2 ####
#------------------------------------------------------------------------------------------------------------------------------------------------------------#
#------------------------------------------------------------------------------------------------------------------------------------------------------------#

### Question 1 - "Would you support or oppose this new trade agreement with [Partner]?"

mod1a <- lm(out_exp2_q1 ~ exp1, data = dfexp2_passac)
mod1b <- lm(out_exp2_q1 ~ exp2, data = dfexp2_passac)
mod1c <- lm(out_exp2_q1 ~ exp1*exp2, data = dfexp2_passac)

summary(mod1a)
summary(mod1b) # note: this effect is reported directly in the main text (Section 5: Results)
summary(mod1c)

mod1a_completedata <- lm(out_exp2_q1 ~ exp1, data = dfexp2)
mod1b_completedata <- lm(out_exp2_q1 ~ exp2, data = dfexp2)
mod1c_completedata <- lm(out_exp2_q1 ~ exp1*exp2, data = dfexp2)

summary(mod1a_completedata)
summary(mod1b_completedata)
summary(mod1c_completedata)

### Question 2 - "Is this new trade agreement fair or unfair for the United States?"

mod2a <- lm(out_exp2_q2 ~ exp1, data = dfexp2_passac)
mod2b <- lm(out_exp2_q2 ~ exp2, data = dfexp2_passac)
mod2c <- lm(out_exp2_q2 ~ exp1*exp2, data = dfexp2_passac)

summary(mod2a) # note: this effect is reported directly in the main text (Section 5: Results)
summary(mod2b) # note: this effect is reported directly in the main text (Section 5: Results)
summary(mod2c)

mod2a_completedata <- lm(out_exp2_q2 ~ exp1, data = dfexp2)
mod2b_completedata <- lm(out_exp2_q2 ~ exp2, data = dfexp2)
mod2c_completedata <- lm(out_exp2_q2 ~ exp1*exp2, data = dfexp2)

summary(mod2a_completedata)
summary(mod2b_completedata)
summary(mod2c_completedata)

### Question 3 - "Is this new trade agreement fair or unfair for [Partner]?"

mod3a <- lm(out_exp2_q3 ~ exp1, data = dfexp2_passac)
mod3b <- lm(out_exp2_q3 ~ exp2, data = dfexp2_passac)
mod3c <- lm(out_exp2_q3 ~ exp1*exp2, data = dfexp2_passac)

summary(mod3a)
summary(mod3b)
summary(mod3c)

mod3a_completedata <- lm(out_exp2_q3 ~ exp1, data = dfexp2)
mod3b_completedata <- lm(out_exp2_q3 ~ exp2, data = dfexp2)
mod3c_completedata <- lm(out_exp2_q3 ~ exp1*exp2, data = dfexp2)

summary(mod3a_completedata)
summary(mod3b_completedata)
summary(mod3c_completedata)


#------------------------------------------------------------------------------------------------------------------------------------------------------------#
#------------------------------------------------------------------------------------------------------------------------------------------------------------#
#### ~ Figures ~ ####
#------------------------------------------------------------------------------------------------------------------------------------------------------------#
#------------------------------------------------------------------------------------------------------------------------------------------------------------#


#### ~~~ Coef Plot Frame ~~~ ####

cplot21 <- broom::tidy(mod1a, conf.int = TRUE, conf.level = 0.95)%>%
  mutate(exp1 = case_when(term == "exp1protectionism" ~ "US Economic\nCosts",
                          term == "exp1sovereignty" ~ "Foreign\nSovereignty")) %>%
  mutate(model = "Would you support or oppose this new trade agreement with [Partner]?")

cplot21

cplot22 <- broom::tidy(mod2a, conf.int = TRUE, conf.level = 0.95)%>%
  mutate(exp1 = case_when(term == "exp1protectionism" ~ "US Economic\nCosts",
                          term == "exp1sovereignty" ~ "Foreign\nSovereignty")) %>%
  mutate(model = "Is this new trade agreement fair or unfair for the United States?")

cplot22

cplot23 <- broom::tidy(mod3a, conf.int = TRUE, conf.level = 0.95)%>%
  mutate(exp1 = case_when(term == "exp1protectionism" ~ "US Economic\nCosts",
                          term == "exp1sovereignty" ~ "Foreign\nSovereignty")) %>%
  mutate(model = "Is this new trade agreement fair or unfair for [Partner]?")

cplot23

cplot2_combo <- rbind(cplot21, cplot22, cplot23) %>%
  drop_na(exp1) %>%
  mutate(model = fct_relevel(model,
                             "Would you support or oppose this new trade agreement with [Partner]?",
                             "Is this new trade agreement fair or unfair for the United States?",
                             "Is this new trade agreement fair or unfair for [Partner]?"))

plot_cplot2 <- ggplot(cplot2_combo, aes(x = estimate, xmin = conf.low, xmax = conf.high, y = exp1)) +
  geom_vline(xintercept = 0, colour = "indianred", lty = "dashed") +
  geom_point(size = 3) +
  geom_linerange(size = 1) +
  scale_x_continuous(limits = c(-0.3, 0.3)) +
  facet_wrap(~model, ncol = 1) +
  theme_bw() +
  theme(
    strip.background = element_rect(fill = "#F3F4F9"),
    strip.text = element_text(face = "bold")
  ) +
  labs(
    x = "Estimate",
    y = ""
  )

plot_cplot2

ggsave(filename = here("2 Figures", "Experiment 2 (Frames) Coefficient Plot (Presentation).png"), plot = plot_cplot2, scale = 1.1)



plot_cplot2_paper <- ggplot(cplot2_combo, aes(x = estimate, xmin = conf.low, xmax = conf.high, y = exp1)) +
  geom_vline(xintercept = 0, colour = "indianred", lty = "dashed") +
  geom_point(size = 3) +
  geom_linerange(size = 1) +
  scale_x_continuous(limits = c(-0.3, 0.3)) +
  scale_y_discrete(labels = c("US Economic\nCosts" = "Digital Protectionism", "Foreign\nSovereignty"="Digital Sovereignty")) +
  facet_wrap(~model, ncol = 1) +
  theme_bw() +
  theme(
    strip.background = element_rect(fill = "#F3F4F9"),
    strip.text = element_text(face = "bold")
  ) +
  labs(
    x = "Estimate",
    y = ""
  )

plot_cplot2_paper

ggsave(filename = here("2 Figures", "Experiment 2 (Frames) Coefficient Plot (Paper).png"), plot = plot_cplot2_paper, scale = 1.1)


#### ~~~ Coef Plot Partner ~~~ ####


cplot31 <- broom::tidy(mod1b, conf.int = TRUE, conf.level = 0.95)%>%
  mutate(exp2 = case_when(term == "exp2china" ~ "China",
                          term == "exp2south korea" ~ "South Korea")) %>%
  mutate(model = "Would you support or oppose this new trade agreement with [Partner]?")

cplot31

cplot32 <- broom::tidy(mod2b, conf.int = TRUE, conf.level = 0.95)%>%
  mutate(exp2 = case_when(term == "exp2china" ~ "China",
                          term == "exp2south korea" ~ "South Korea")) %>%
  mutate(model = "Is this new trade agreement fair or unfair for the United States?")

cplot32

cplot33 <- broom::tidy(mod3b, conf.int = TRUE, conf.level = 0.95)%>%
  mutate(exp2 = case_when(term == "exp2china" ~ "China",
                          term == "exp2south korea" ~ "South Korea")) %>%
  mutate(model = "Is this new trade agreement fair or unfair for [Partner]?")

cplot33

cplot3_combo <- rbind(cplot31, cplot32, cplot33) %>%
  drop_na(exp2) %>%
  mutate(model = fct_relevel(model,
                             "Would you support or oppose this new trade agreement with [Partner]?",
                             "Is this new trade agreement fair or unfair for the United States?",
                             "Is this new trade agreement fair or unfair for [Partner]?"))

plot_cplot3 <- ggplot(cplot3_combo, aes(x = estimate, xmin = conf.low, xmax = conf.high, y = exp2)) +
  geom_vline(xintercept = 0, colour = "indianred", lty = "dashed") +
  geom_point(size = 3) +
  geom_linerange(size = 1) +
  scale_x_continuous(limits = c(-0.6, 0.2)) +
  facet_wrap(~model, ncol = 1) +
  theme_bw() +
  theme(
    strip.background = element_rect(fill = "#F3F4F9"),
    strip.text = element_text(face = "bold")
  ) +
  labs(
    x = "Estimate",
    y = ""
  )

plot_cplot3

ggsave(filename = here("2 Figures", "Experiment 2 (Partner) Coefficient Plot (Presentation).png"), plot = plot_cplot3, scale = 1.1)



plot_cplot3_paper <- ggplot(cplot3_combo, aes(x = estimate, xmin = conf.low, xmax = conf.high, y = exp2)) +
  geom_vline(xintercept = 0, colour = "indianred", lty = "dashed") +
  geom_point(size = 3) +
  geom_linerange(size = 1) +
  scale_x_continuous(limits = c(-0.6, 0.2)) +
  facet_wrap(~model, ncol = 1) +
  theme_bw() +
  theme(
    strip.background = element_rect(fill = "#F3F4F9"),
    strip.text = element_text(face = "bold")
  ) +
  labs(
    x = "Estimate",
    y = ""
  )

plot_cplot3_paper

ggsave(filename = here("2 Figures", "Experiment 2 (Partner) Coefficient Plot (Paper).png"), plot = plot_cplot3, scale = 1.1)




#### ~~~~ Combo Plot ~~~~ ####


cplot2_combo <- rbind(cplot21, cplot22, cplot23) %>%
  drop_na(exp1) %>%
  mutate(model = case_when(model == "Would you support or oppose this new trade agreement with [Partner]?" ~ "Would you support or oppose this new \ntrade agreement with [Partner]?",
                           model == "Is this new trade agreement fair or unfair for the United States?" ~ "Is this new trade agreement fair or unfair \nfor the United States?",
                           model == "Is this new trade agreement fair or unfair for [Partner]?" ~ "Is this new trade agreement fair or unfair \nfor [Partner]?")) %>%
  mutate(model = fct_relevel(model,
                             "Would you support or oppose this new \ntrade agreement with [Partner]?",
                             "Is this new trade agreement fair or unfair \nfor the United States?",
                             "Is this new trade agreement fair or unfair \nfor [Partner]?"))


plot_cplot2_paper <- ggplot(cplot2_combo, aes(x = estimate, xmin = conf.low, xmax = conf.high, y = exp1)) +
  geom_vline(xintercept = 0, colour = "indianred", lty = "dashed") +
  geom_point(size = 3) +
  geom_linerange(size = 1) +
  scale_x_continuous(limits = c(-0.3, 0.3)) +
  scale_y_discrete(labels = c("US Economic\nCosts" = "Digital Protectionism", "Foreign\nSovereignty"="Digital Sovereignty")) +
  facet_wrap(~model, ncol = 1) +
  theme_bw() +
  theme(
    strip.background = element_rect(fill = "#F3F4F9"),
    strip.text = element_text(face = "bold")
  ) +
  labs(
    x = "Estimate",
    y = ""
  )

plot_cplot2_paper

###

cplot3_combo <- rbind(cplot31, cplot32, cplot33) %>%
  drop_na(exp2) %>%
  mutate(model = case_when(model == "Would you support or oppose this new trade agreement with [Partner]?" ~ "Would you support or oppose this new \ntrade agreement with [Partner]?",
                           model == "Is this new trade agreement fair or unfair for the United States?" ~ "Is this new trade agreement fair or unfair \nfor the United States?",
                           model == "Is this new trade agreement fair or unfair for [Partner]?" ~ "Is this new trade agreement fair or unfair \nfor [Partner]?")) %>%
  mutate(model = fct_relevel(model,
                             "Would you support or oppose this new \ntrade agreement with [Partner]?",
                             "Is this new trade agreement fair or unfair \nfor the United States?",
                             "Is this new trade agreement fair or unfair \nfor [Partner]?"))

plot_cplot3_paper <- ggplot(cplot3_combo, aes(x = estimate, xmin = conf.low, xmax = conf.high, y = exp2)) +
  geom_vline(xintercept = 0, colour = "indianred", lty = "dashed") +
  geom_point(size = 3) +
  geom_linerange(size = 1) +
  scale_x_continuous(limits = c(-0.6, 0.2)) +
  facet_wrap(~model, ncol = 1) +
  theme_bw() +
  theme(
    strip.background = element_rect(fill = "#F3F4F9"),
    strip.text = element_text(face = "bold")
  ) +
  labs(
    x = "Estimate",
    y = ""
  )

plot_cplot3_paper

###

cplot_combo_paper <- ggpubr::ggarrange(plot_cplot2_paper, plot_cplot3_paper,
                  nrow = 1,
                  labels = "AUTO")

ggsave(filename = here("2 Figures", "Experiment 2 Main Effects Coefficient Plot (Paper).png"), plot = cplot_combo_paper, scale = 1.1)



#### ~~~ Marginal Effects Crossed ~~~ ####

## Question 1

plotint1paper <- plot_predictions(mod1c, by = c("exp2", "exp1"), conf_level = 0.95, draw = FALSE) %>%
  mutate(exp1 = case_when(exp1 == "control" ~ "Control",
                          exp1 == "protectionism" ~ "Digital\nProtectionism",
                          exp1 == "sovereignty" ~ "Digital\nSovereignty")) %>%
  mutate(exp2 = case_when(exp2 == "canada" ~ "Canada",
                          exp2 == "china" ~ "China",
                          exp2 == "south korea" ~ "South Korea"))

plotint1paper <- ggplot(plotint1paper, aes(x = exp2, y = estimate, ymin = conf.low, ymax = conf.high)) +
  geom_pointrange(aes(color = exp1, shape = exp1), stat = "identity", position = position_dodge2(width = .5), size = 0.5) +
  scale_y_continuous(limits = c(-0.1, 1)) +      
  scale_color_manual(values = c("black", "#d55e00", "#0072b2")) +
  labs(
    x = "",
    y = "Predicted Value",
    title = "Would you support or oppose this new trade agreement with [Partner]?"
  ) +
  theme_minimal() +
  theme(
    legend.title = element_blank(),
    legend.position = "bottom"
  )

plotint1paper

## Question 2

plotint2paper <- plot_predictions(mod2c, by = c("exp2", "exp1"), conf_level = 0.95, draw = FALSE) %>%
  mutate(exp1 = case_when(exp1 == "control" ~ "Control",
                          exp1 == "protectionism" ~ "Digital\nProtectionism",
                          exp1 == "sovereignty" ~ "Digital\nSovereignty")) %>%
  mutate(exp2 = case_when(exp2 == "canada" ~ "Canada",
                          exp2 == "china" ~ "China",
                          exp2 == "south korea" ~ "South Korea"))

plotint2paper <- ggplot(plotint2paper, aes(x = exp2, y = estimate, ymin = conf.low, ymax = conf.high)) +
  geom_pointrange(aes(color = exp1, shape = exp1), stat = "identity", position = position_dodge2(width = .5), size = 0.5) +
  scale_y_continuous(limits = c(-0.1, 1)) +      
  scale_color_manual(values = c("black", "#d55e00", "#0072b2")) +
  labs(
    x = "",
    y = "Predicted Value",
    title = "Is this new trade agreement fair or unfair for the United States?"
  ) +
  theme_minimal() +
  theme(
    legend.title = element_blank(),
    legend.position = "bottom"
  )

plotint2paper

## Question 3

plotint3paper <- plot_predictions(mod3c, by = c("exp2", "exp1"), conf_level = 0.95, draw = FALSE) %>%
  mutate(exp1 = case_when(exp1 == "control" ~ "Control",
                          exp1 == "protectionism" ~ "Digital\nProtectionism",
                          exp1 == "sovereignty" ~ "Digital\nSovereignty")) %>%
  mutate(exp2 = case_when(exp2 == "canada" ~ "Canada",
                          exp2 == "china" ~ "China",
                          exp2 == "south korea" ~ "South Korea"))

plotint3paper <- ggplot(plotint3paper, aes(x = exp2, y = estimate, ymin = conf.low, ymax = conf.high)) +
  geom_pointrange(aes(color = exp1, shape = exp1), stat = "identity", position = position_dodge2(width = .5), size = 0.5) +
  scale_y_continuous(limits = c(-0.1, 1)) +      
  scale_color_manual(values = c("black", "#d55e00", "#0072b2")) +
  labs(
    x = "",
    y = "Predicted Value",
    title = "Is this new trade agreement fair or unfair for [Partner]?"
  ) +
  theme_minimal() +
  theme(
    legend.title = element_blank(),
    legend.position = "bottom"
  )

plotint3paper

plotint_combo <- ggarrange(plotint1paper, plotint2paper, plotint3paper,
                  ncol = 1,
                  common.legend = TRUE,
                  legend = "bottom")

ggsave(filename = here("2 Figures", "Experiment 2 (Crossed) Effects Plot - All Questions (Paper).png"), plot = plotint_combo, scale = 1.1)


#------------------------------------------------------------------------------------------------------------------------------------------------------------#
#------------------------------------------------------------------------------------------------------------------------------------------------------------#
#### ~ Tables ~ ####
#------------------------------------------------------------------------------------------------------------------------------------------------------------#
#------------------------------------------------------------------------------------------------------------------------------------------------------------#

## Q1

modsac <- list(
  mod1a, 
  mod1a_completedata, 
  mod1b,
  mod1b_completedata,
  mod1c,
  mod1c_completedata
)

cm <- c(
  '(Intercept)'='Intercept',
  'exp1protectionism'='Digital Protectionism',
  'exp1sovereignty'='Digital Sovereignty',
  'exp2china'='China',
  'exp2south korea'='South Korea',
  'exp1protectionism:exp2china'='Digital Protectionism x China',
  'exp1sovereignty:exp2china'='Digital Sovereignty x China',
  'exp1protectionism:exp2south korea'='Digital Protectionism x South Korea',
  'exp1sovereignty:exp2south korea'='Digital Sovereignty x South Korea'
)

rows <- tribble(~term,          ~"(1)",  ~"(2)", ~"(3)",  ~"(4)", ~"(5)",  ~"(6)", 
                'Attention Check',         'Pass', 'All','Pass', 'All','Pass', 'All')
attr(rows, 'position') <- c(11)

tab <- modelsummary(modsac,
                    output = "gt",
                    stars = TRUE,
                    coef_map = cm,
                    estimate = "{estimate} ({std.error}){stars}",
                    statistic = NULL,
                    add_rows = rows,
                    gof_map = c("nobs", "r.squared"))

tab


gt::gtsave(tab, filename = here("3 Tables", "Experiment 2 Table 1.docx"))

## Q2

modsac <- list(
  mod2a, 
  mod2a_completedata, 
  mod2b,
  mod2b_completedata,
  mod2c,
  mod2c_completedata
)

cm <- c(
  '(Intercept)'='Intercept',
  'exp1protectionism'='Digital Protectionism',
  'exp1sovereignty'='Digital Sovereignty',
  'exp2china'='China',
  'exp2south korea'='South Korea',
  'exp1protectionism:exp2china'='Digital Protectionism x China',
  'exp1sovereignty:exp2china'='Digital Sovereignty x China',
  'exp1protectionism:exp2south korea'='Digital Protectionism x South Korea',
  'exp1sovereignty:exp2south korea'='Digital Sovereignty x South Korea'
)

rows <- tribble(~term,          ~"(1)",  ~"(2)", ~"(3)",  ~"(4)", ~"(5)",  ~"(6)", 
                'Attention Check',         'Pass', 'All','Pass', 'All','Pass', 'All')
attr(rows, 'position') <- c(11)

tab <- modelsummary(modsac,
                    output = "gt",
                    stars = TRUE,
                    coef_map = cm,
                    estimate = "{estimate} ({std.error}){stars}",
                    statistic = NULL,
                    add_rows = rows,
                    gof_map = c("nobs", "r.squared"))

tab


gt::gtsave(tab, filename = here("3 Tables", "Experiment 2 Table 2.docx"))


## Q3

modsac <- list(
  mod3a, 
  mod3a_completedata, 
  mod3b,
  mod3b_completedata,
  mod3c,
  mod3c_completedata
)

cm <- c(
  '(Intercept)'='Intercept',
  'exp1protectionism'='Digital Protectionism',
  'exp1sovereignty'='Digital Sovereignty',
  'exp2china'='China',
  'exp2south korea'='South Korea',
  'exp1protectionism:exp2china'='Digital Protectionism x China',
  'exp1sovereignty:exp2china'='Digital Sovereignty x China',
  'exp1protectionism:exp2south korea'='Digital Protectionism x South Korea',
  'exp1sovereignty:exp2south korea'='Digital Sovereignty x South Korea'
)

rows <- tribble(~term,          ~"(1)",  ~"(2)", ~"(3)",  ~"(4)", ~"(5)",  ~"(6)", 
                'Attention Check',         'Pass', 'All','Pass', 'All','Pass', 'All')
attr(rows, 'position') <- c(11)

tab <- modelsummary(modsac,
                    output = "gt",
                    stars = TRUE,
                    coef_map = cm,
                    estimate = "{estimate} ({std.error}){stars}",
                    statistic = NULL,
                    add_rows = rows,
                    gof_map = c("nobs", "r.squared"))

tab


gt::gtsave(tab, filename = here("3 Tables", "Experiment 2 Table 3.docx"))


#------------------------------------------------------------------------------------------------------------------------------------------------------------#
#------------------------------------------------------------------------------------------------------------------------------------------------------------#
#### ~ Contrasts ~ ####
#------------------------------------------------------------------------------------------------------------------------------------------------------------#
#------------------------------------------------------------------------------------------------------------------------------------------------------------#



avg_comparisons(
  mod1c,
  variables = "exp1",
  by = "exp2"
)

# note: this is cited in the main text (Section 5: Results)
# protectionism - control (Canada) = -0.1972 (p = 0.0246)
# sovereignty - control (China) = 0.1557 (p = 0.0765)

avg_comparisons(
  mod2c,
  variables = "exp1",
  by = "exp2"
)

# note: this is cited in the main text (Section 5: Results)
# protectionism - control (Canada) = -0.16213 (p = 0.05899)
# sovereignty - control (China) = 0.22308 (p = 0.00952)


avg_comparisons(
  mod3c,
  variables = "exp1",
  by = "exp2"
)

#------------------------------------------------------------------------------------------------------------------------------------------------------------#
#------------------------------------------------------------------------------------------------------------------------------------------------------------#
#### End of Script ####
#------------------------------------------------------------------------------------------------------------------------------------------------------------#
#------------------------------------------------------------------------------------------------------------------------------------------------------------#

